package com.group.transaction.aspect;

import com.alibaba.fastjson.JSON;
import com.group.transaction.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.Objects;

/**
 * controller切面
 *
 * @author YunXing Pang
 * @date 2020/4/3
 */
@Aspect
@Component
@Slf4j
public class ControllerAspect {

    @Pointcut("execution(* com.group.transaction.controller.*.*(..))")
    public void pointcut() {

    }

    @Before(value = "pointcut()")
    public void before(JoinPoint joinPoint) {
        String className = joinPoint.getTarget().getClass().getName();
        Object[] objects = joinPoint.getArgs();
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();

        InMethod inMethod = method.getAnnotation(InMethod.class);
        String apiName = Objects.nonNull(inMethod) ? inMethod.apiName() : "请求接口";

        RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
        GetMapping getMapping = method.getAnnotation(GetMapping.class);
        PostMapping postMapping = method.getAnnotation(PostMapping.class);
        PutMapping putMapping = method.getAnnotation(PutMapping.class);
        DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class);
        String methodUrl = getUrl(requestMapping, getMapping, postMapping, putMapping, deleteMapping);
        String classUrl = "";
        try {
            Class clazz = Class.forName(className);
            requestMapping = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
            getMapping = (GetMapping) clazz.getAnnotation(GetMapping.class);
            postMapping = (PostMapping) clazz.getAnnotation(PostMapping.class);
            putMapping = (PutMapping) clazz.getAnnotation(PutMapping.class);
            deleteMapping = (DeleteMapping) clazz.getAnnotation(DeleteMapping.class);
            classUrl = getUrl(requestMapping, getMapping, postMapping, putMapping, deleteMapping);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        log.info("{}：{}{}，请求方法：{}，请求参数：{}", apiName, classUrl, methodUrl, method.getName(), JSON.toJSONString(objects));
    }

    private String getUrl(RequestMapping requestMapping, GetMapping getMapping, PostMapping postMapping, PutMapping putMapping, DeleteMapping deleteMapping) {
        String url;
        if (Objects.nonNull(requestMapping)) {
            url = requestMapping.value()[0];
        } else if (Objects.nonNull(getMapping)) {
            url = getMapping.value()[0];
        } else if (Objects.nonNull(postMapping)) {
            url = postMapping.value()[0];
        } else if (Objects.nonNull(putMapping)) {
            url = putMapping.value()[0];
        } else if (Objects.nonNull(deleteMapping)) {
            url = deleteMapping.value()[0];
        } else {
            url = "";
        }
        if (StringUtil.isNotEmpty(url) && url.charAt(0) != '/') {
            url = "/" + url;
        }
        return url;
    }

}
